package com.miraclengine.xml;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.HashMap;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.miraclengine.utils.Debug;
import com.miraclengine.utils.GlobalStatic;

public class XmlDataTableParser {
	
	private String TAG = XmlDataTableParser.class.getSimpleName();
	
	private DataTable dt = new DataTable();
	private DataRow dr = null; 
	private String[] loopTags;
	
	public String encoding = "UTF-8";
	
	public void setDt(DataTable dt) {
		this.dt = dt;
	}
	
	public DataTable getDt() {
		return this.dt;
	}
	
	public void setTagLoop(String... tagLoops) {
		loopTags = new String[tagLoops.length];
		for(int i=0 ; i<tagLoops.length ; i++) {
			loopTags[i] = tagLoops[i];
			Debug.d(TAG, "loopTags[" + i + "] : " + loopTags[i]);
		}
	}
	
	public void startParsing(String tempData) {
		
		String tmpData = tempData.replace("\n", "");
		tmpData = tmpData.replace("\t", "");
		tmpData = tmpData.replaceAll(GlobalStatic.newLineValue, "\n");
		
		try {
			DocumentBuilderFactory docBF = DocumentBuilderFactory.newInstance();
			DocumentBuilder docB = docBF.newDocumentBuilder();
			InputStream is = new ByteArrayInputStream(tmpData.getBytes());
			
			Document doc = docB.parse(is);
			
			Element lists = doc.getDocumentElement();
			
			
			NodeList resultList = lists.getElementsByTagName("result");
			Node resultNode = resultList.item(0);
			if(resultNode.getFirstChild() != null) {
				GlobalStatic.LAST_EXCUTE_RESULT_CODE = resultNode.getFirstChild().getNodeValue();
				Debug.d(TAG, GlobalStatic.LAST_EXCUTE_RESULT_CODE);
			}
			
			NodeList msgList = lists.getElementsByTagName("msg");
			Node msgNode = msgList.item(0);
			if(msgNode.getFirstChild() != null) {
				GlobalStatic.LAST_EXCUTE_RESULT_DESCRIPTION = msgNode.getFirstChild().getNodeValue();
				Debug.d(TAG, GlobalStatic.LAST_EXCUTE_RESULT_DESCRIPTION);
			}
			
			NodeList bodyList = lists.getElementsByTagName("msgBody");
			NodeList fList = bodyList.item(0).getChildNodes();
			
			for(int i=0 ; i<fList.getLength() ; i++) {
				NodeList dList = fList.item(i).getChildNodes();
				String dNodeName = fList.item(i).getNodeName();
				
				dr = dt.newRow();
				HashMap<String, String> hm = new HashMap<String, String>();
				for(int j=0 ; j<dList.getLength() ; j++) {
					Node dNode = dList.item(j);
					String dName = dNode.getNodeName();
					String dValue = dNode.getFirstChild().getNodeValue();
					
					dt.addColumn(dName);
					
					hm.put(dName, dValue);
				}

				dr.put(dNodeName + i, hm);
				dt.addRow(dr);
			}
			
		
			
		} catch(Exception e) {
			Debug.d(TAG, e.getMessage());
		}
	}
	
	public void startParsing2(String tmpData) {
		try {
			DocumentBuilderFactory docBF = DocumentBuilderFactory.newInstance();
			DocumentBuilder docB = docBF.newDocumentBuilder();
			InputStream is = new ByteArrayInputStream(tmpData.getBytes());
			
			Document doc = docB.parse(is);
			
			Element lists = doc.getDocumentElement();
			
			//Header처리 
			NodeList rLists = lists.getElementsByTagName(loopTags[0]);
			//Debug.d(TAG, "rLists Length : " + rLists.getLength());
			for(int i=0 ; i<rLists.getLength() ; i++) {
				NodeList tempNodeList = rLists.item(i).getChildNodes();
				//Debug.d(TAG, "tempNodeList Length : " + tempNodeList.getLength());
				
				dr = dt.newRow();
				
				for(int j=0 ; j<tempNodeList.getLength() ; j++) {
					Node tempNode = tempNodeList.item(j);
					
					if(tempNode.getFirstChild() != null) {
						String tempName = tempNode.getNodeName();
						String tempValue = tempNode.getFirstChild().getNodeValue();
						
						dt.addColumn(tempName);
						
						boolean bFlag = false;
						
						for(int k=1 ; k<loopTags.length ; k++) {
							if(tempName.equals(loopTags[k])) {
								HashMap<String, String> hm = new HashMap<String, String>();
								NodeList tagNodeList = tempNode.getChildNodes();
								for(int l=0 ; l<tagNodeList.getLength() ; l++) {
									Node tagNode = tagNodeList.item(l);
									if(tagNode.getFirstChild() != null) {
										String tagName = tagNode.getNodeName();
										String tagValue = tagNode.getFirstChild().getNodeValue();
										
										hm.put(tagName, tagValue);
										//Debug.d(TAG, "result -> " + tagName + " : " + tagValue);
									}
								}
								dr.put(tempName, hm);
								bFlag = true;
								break;
							}
						}
						
						if(!bFlag) {
							dr.put(tempName, tempValue);
							//Debug.d(TAG, "result -> " + tempName + " : " + tempValue);		
						}
					}
				}
				
				dt.addRow(dr);
			}
		} catch(Exception e) {
			Debug.d(TAG, e.getMessage());
		}
	}

}
